package dat255.calendarclock.view;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.TreeSet;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import dat255.calendarclock.R;
import dat255.calendarclock.model.event.SubEvent;

/**
 * Adapter for the list views
 * @author Marcus Hultman
 *
 */
class ListCalendarAdapter extends BaseAdapter {
	
	//Fields

	private static final int TYPE_ITEM = 0;
	private static final int TYPE_SEPARATOR = 1;
	private static final int TYPE_NOEVENTS = 2;
	private static final int TYPE_MAX_COUNT = 3;

	@SuppressWarnings("rawtypes")
	private ArrayList mData = new ArrayList();
	private LayoutInflater layoutInflater;

	// Should we diplay a labal saying there're no events?
	private boolean noEvents;
	
	// We save the index of all mData items that in fact is separators
	@SuppressWarnings("rawtypes")
	private TreeSet mSeparatorsSet = new TreeSet();

	public ListCalendarAdapter(LayoutInflater layoutInflater) {
		this.layoutInflater = layoutInflater;
	}

	@SuppressWarnings("unchecked")
	public void addItem(final SubEvent item) {
		mData.add(item);
		notifyDataSetChanged();
	}

	@SuppressWarnings("unchecked")
	public void addSeparatorItem(final GregorianCalendar item) {
		mData.add(item);
		// save separator position
		mSeparatorsSet.add(mData.size() - 1);
		notifyDataSetChanged();
	}

	@SuppressWarnings("unchecked")
	public void setNoEvents(boolean noEvents){
		mData.clear();
		mData.add(TYPE_NOEVENTS);
		this.noEvents = noEvents;
	}
	
	@Override
	public boolean isEnabled(int position) {
		return !noEvents && !mSeparatorsSet.contains(position);
	}
	
	@Override
	public int getItemViewType(int position) {
		if (noEvents)
			return TYPE_NOEVENTS;
		return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
	}

	@Override
	public int getViewTypeCount() {
		return TYPE_MAX_COUNT;
	}

	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ListItemHolder holder = null;

		int type = getItemViewType(position);
		
		if (type == TYPE_NOEVENTS){
			if (convertView == null)
				convertView = layoutInflater.inflate(R.layout.listnoevents_layout,
						null);
			return convertView;
		}
		if (convertView == null) {
			SimpleDateFormat simplerDateFormat;
			holder = new ListItemHolder();
			switch (type) {
				case TYPE_ITEM:
					convertView = layoutInflater.inflate(R.layout.listitem_layout,
							null);
	
					View label = holder.label = (View) convertView
							.findViewById(R.id.listItem_Label);
					TextView timeText = holder.timeText = (TextView) convertView
							.findViewById(R.id.listItem_Time);
					TextView nameText = holder.nameText = (TextView) convertView
							.findViewById(R.id.listItem_Name);
	
					SubEvent subEvent = (SubEvent) mData.get(position);
					simplerDateFormat = new SimpleDateFormat("kk:mm");
	
					if (label != null) {
						label.setBackgroundColor(subEvent.getEvent().getCategory().getColor());
					}
					if (timeText != null) {
						timeText.setText(simplerDateFormat.format(subEvent
								.getDate().getTime()));
					}
					if (nameText != null) {
						nameText.setText(subEvent.getEvent().getName());
					}
					break;
				case TYPE_SEPARATOR:
					convertView = layoutInflater.inflate(
							R.layout.listseparator_layout, null);
	
					TextView text = holder.text = (TextView) convertView
							.findViewById(R.id.listSeparator_Text);
					
					GregorianCalendar date = (GregorianCalendar) mData
							.get(position);
					simplerDateFormat = new SimpleDateFormat("dd MMMM yyyy");
	
					if (text != null) {
						text.setText(simplerDateFormat.format(date.getTime()));
					}
					break;
			}
			convertView.setTag(holder);
		} else {
			SimpleDateFormat simplerDateFormat;
			holder = (ListItemHolder) convertView.getTag();
			switch (type) {
				case TYPE_ITEM:
	
					SubEvent subEvent = (SubEvent) mData.get(position);
					simplerDateFormat = new SimpleDateFormat("kk:mm");
	
					holder.label.setBackgroundColor(subEvent.getEvent().getCategory().getColor());
					holder.timeText.setText(simplerDateFormat.format(subEvent
							.getDate().getTime()));
					holder.nameText.setText(subEvent.getEvent().getName());
					break;
				case TYPE_SEPARATOR:
					GregorianCalendar date = (GregorianCalendar) mData
							.get(position);
					simplerDateFormat = new SimpleDateFormat("dd MMMM yyyy");
	
					holder.text.setText(simplerDateFormat.format(date.getTime()));
					break;
			}

		}
		return convertView;
	}

}